using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace HelloXNA2.Component.MyCustom.FourPolygon
{
    public class Demo : Microsoft.Xna.Framework.DrawableGameComponent
    {
        public Demo(Game game)
            : base(game)
        {
            // TODO: Construct any child components here
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here

            base.Update(gameTime);
        }

        PolygonHelper _polygonHelper;
        static float tick = 0;
        public override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            if (_polygonHelper == null)
                _polygonHelper = new PolygonHelper(GraphicsDevice, FourPolygon.Corners, FourPolygon.Faces, new Color(1f, 0f, 0f));

            var _aspectRatio = (float)Game.Window.ClientBounds.Width / (float)Game.Window.ClientBounds.Height;
            Matrix view = Matrix.CreateLookAt(new Vector3(0, 0, 8.0f), Vector3.Zero, Vector3.Up);
            Matrix projection = Matrix.CreatePerspectiveFieldOfView(0.85f, _aspectRatio, 0.01f, 1000.0f);

            Vector3 axis = new Vector3(-0.5f, 1, -0.5f);
            axis.Normalize();


            _polygonHelper.World = Matrix.CreateFromQuaternion(Quaternion.CreateFromAxisAngle(axis, (float)(tick = tick + 0.03f)));
            GraphicsDevice.Clear(new Color(0.8f, 0.8f, 0.8f, 1.0f));

            _polygonHelper.Draw(view, projection);
            base.Draw(gameTime);
        }
    }
}
